package com.jackchong.net

import com.jackchong.base.BaseView
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * author : chong.huang
* create : 4/1/21
 * desc   :
 */
open class JBaseNet {

    fun updateImage(path: List<String>, view: BaseView, block: (path: List<String>) -> Unit) {

        val builder = MultipartBody.Builder().setType(MultipartBody.FORM)

        val needList = path.toMutableList()
        val list = ArrayList<String>()
        for (i in path.indices) {
            if (!path[i].startsWith("http")){
                val file = File(path[i])
                list.add(path[i])
                builder.addFormDataPart("file", file.name, file.asRequestBody("multipart/form-data".toMediaTypeOrNull()))
            }
        }

        if (list.isEmpty()){
            //没有需要上传的图片, 直接返回url
            return block(path)
        }

        val body = builder.build()

/*
        view.net(Api.service.uploads(body)) {
            val toMutableList = it?.toMutableList()?: mutableListOf()
            for (i in needList.indices) {
                val file = File(needList[i])
                if (file.exists()){
                    needList[i] = toMutableList.removeAt(0)
                }
            }
            block(needList)
        }
*/
    }
}